/**
 * Created by futao on 1/4/2016.
 */
angular.module('sbAdminApp')
    .service('statisticaorderService',['$http','httpResource', function ($http, httpResource) {
    	
     var func="sc/rpt/order";    
        
        this.search1 = function (sorder,beginDate,endDate, pageno, itempersize) {
        	var name=null; var id=null;  var sc=null;
        	if(sorder !=null && sorder !=undefined){
        		name=sorder.settlementTargetName;
            	id=sorder.orderId;
            	 if(name==undefined){
             	 	   name="";
             	    }
           	     if(id==undefined){
           	    	id="";
           	     }
	           	 if(sorder.settlementCategory !=undefined){
	      	    	sc=sorder.settlementCategory;
	      	     }
        	}
            
       	  return  $http({
              url: func+'/statistica',
              method: 'post',
              params: { 
           	        orderId:id,
           	        settlementTargetName :name,
           	        settlementCategory: sc,
           	        pageNo: pageno,
        	        pageSize: itempersize,
           	        beginTime: beginDate,
           	        endTime: endDate
           	       },
          });
       	     
        };

        //修改金额
        this.edit1 = function (id, settlementSum) {
        	return $http({
              url: func+'/editSettlementSum',
              method: 'post',
              params: {id: id,
            	       settlementSum: settlementSum
           	         },
             });
        };
        
        
        this.export1=function (sorder, beginDate, endDate, pageno, itempersize,currentPage ) {
        	var name=null;  var id=null;  var sc=null;  
        	if(sorder!=null && sorder!=undefined ){
            	 if(sorder.settlementTargetName!=undefined){
             	 	name=sorder.settlementTargetName;
             	  }
           	     if(sorder.orderId !=undefined){
           	    	id=sorder.orderId;
           	     }
           	     if(sorder.settlementCategory !=undefined){
        	    	sc=sorder.settlementCategory;
        	     }
        	}
        	
     	   $http({
               url: func+'/export',
               method: 'post',
               params: { 
	            	    orderId:id,
	           	        settlementTargetName: name,
	           	        settlementCategory: sc,
	           	        pageNo: pageno,
	        	        pageSize: itempersize,
	           	        beginTime: beginDate,
	           	        endTime: endDate,
	           	        currentPage: currentPage
            	       },
               responseType: 'arraybuffer'
           }).success(function (data, status, headers) {
        	   
//               ele.prop('disabled', false);
               var type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
//               switch (attr.downFileType) {
//                   case 'xlsx':
//                       type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
//                       break;
//               }
               if (!type) throw '无效类型';
               saveAs(new Blob([data], { type: type }), decodeURI(headers()["x-filename"]));  // 中文乱码
           
           }).error(function (data, status) {
               alert("导出失败!");
           });
        };
        
        
  
    }]);
